package com.zxl.yjb.activity.my;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.zxl.yjb.R;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.fragment.my.ImageEditFragment;
import com.zxl.yjb.utils.DisplayUtil;

import java.util.ArrayList;

import io.rong.imkit.plugin.image.HackyViewPager;

/**
 * 图片查看器
 */
public class ImageEditActivity extends BaseActivity {
    private static final String STATE_POSITION = "STATE_POSITION";
    public static final String EXTRA_IMAGE_INDEX = "image_index";
    public static final String EXTRA_IMAGE_URLS = "image_urls";
    private HackyViewPager mPager;
    private int pagerPosition;
    private TextView indicator;
    private ImageView ivBack;
    private TextView ivDelete;
    private View llHeader;
    private ObjectAnimator anima;
    private int height;
    private boolean isOpen = true;
    private ArrayList<String> urls;
    private ImagePagerAdapter mAdapter;
    private ArrayList<Integer> deletepage = new ArrayList<>();

    @Override
    protected void initView() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_edit_pager);

        pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);
        urls = getIntent().getStringArrayListExtra(EXTRA_IMAGE_URLS);

        mPager = (HackyViewPager) findViewById(R.id.pager);
        llHeader = (View) findViewById(R.id.ll_header);
        mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), urls);
        mPager.setAdapter(mAdapter);
        indicator = (TextView) findViewById(R.id.tv_page_title);
        indicator.setText(getString(R.string.viewpager_indicator));
        ivBack = (ImageView) findViewById(R.id.iv_back);
        ivDelete = (TextView) findViewById(R.id.tv_confirm);
        ivDelete.setVisibility(View.VISIBLE);
        ivDelete.setText("删除");
        llHeader.post(new Runnable() {
            @Override
            public void run() {
                height = llHeader.getHeight();
            }
        });
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SaveAndBack();

            }
        });
        ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showRemind();
            }
        });
        CharSequence text = getString(R.string.viewpager_indicator, 1, mPager.getAdapter().getCount());
        indicator.setText(text);
        // 更新下标

        mPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                CharSequence text = getString(R.string.viewpager_indicator, arg0 + 1, mPager.getAdapter().getCount());
                indicator.setText(text);
            }

            @Override
            public void onPageSelected(int arg0) {
                CharSequence text = getString(R.string.viewpager_indicator, arg0 + 1, mPager.getAdapter().getCount());
                indicator.setText(text);
            }

        });
        if (savedInstanceState != null) {
            pagerPosition = savedInstanceState.getInt(STATE_POSITION);
        }

        mPager.setCurrentItem(pagerPosition);
    }

    private void SaveAndBack() {
        Intent intent = new Intent();
        intent.putExtra("delete_item", deletepage);
        setResult(RESULT_OK, intent);
        finish();
    }

    private void showRemind() {
        /*final WeixinDialog weixinDialog = new WeixinDialog(ImageEditActivity.this);
        weixinDialog.setMessage("要删除这张照片吗？");
        weixinDialog.setNoOnclickListener("取消", new WeixinDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                weixinDialog.cancel();
            }
        });
        weixinDialog.setYesOnclickListener("确定", new WeixinDialog.onYesOnclickListener() {
            @Override
            public void onYesClick() {
                int selecteItem = mPager.getCurrentItem();
                Intent intent = new Intent();
                intent.putExtra("delete_item", selecteItem);
                setResult(RESULT_OK, intent);
                ImageEditActivity.this.finish();
            }
        });
        weixinDialog.show();*/
        View.OnClickListener okListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int selecteItem = mPager.getCurrentItem();
                urls.remove(selecteItem);
                deletepage.add(selecteItem);
                if (urls.size() == 0) {     //一张图片也没有就关闭页面
                   SaveAndBack();
                }else {
                    mAdapter.notifyDataSetChanged();
                }

            }
        };

        DisplayUtil.diaplayDialog(ImageEditActivity.this, "提示", "要删除这张照片吗?","取消","确定" ,null,okListener);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_POSITION, mPager.getCurrentItem());
    }

    private class ImagePagerAdapter extends FragmentStatePagerAdapter {

        public ArrayList<String> fileList;

        public ImagePagerAdapter(FragmentManager fm, ArrayList<String> fileList) {
            super(fm);
            this.fileList = fileList;
        }

        @Override
        public int getCount() {
            return fileList == null ? 0 : fileList.size();
        }

        @Override
        public Fragment getItem(int position) {
//			String url = fileList.get(position).replace("-small","");//去掉-small就是大图；
            String url = fileList.get(position);
            return ImageEditFragment.newInstance("file://" + url);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }


    public void close() {
        if (anima != null && anima.isRunning()) {
            return;
        }
        anima = ObjectAnimator.ofFloat(llHeader, "translationY", 0, -height);
        anima.setDuration(100);
        anima.start();
        isOpen = false;
    }

    public void open() {

        if (anima != null && anima.isRunning()) {
            return;
        }
        anima = ObjectAnimator.ofFloat(llHeader, "translationY", -height, 0);
        anima.setDuration(100);
        anima.start();
        isOpen = true;
    }

    public void onImageClick() {
        if (isOpen) {
            close();
        } else {
            open();
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode== KeyEvent.KEYCODE_BACK){
           SaveAndBack();
        }
        return true;
    }


}
